import pathToRegexp from 'path-to-regexp/index';
import {getCheck,postCheck,deleteChack,putCheck,getInspep,getPoint} from 'services/app';
import { routerRedux } from 'dva/router';
export default {
    namespace: 'check',
    state: {
        pages:1,
        record:{},
    },
    subscriptions: {
        setupHistory ({ dispatch, history }) {
            history.listen((location) => {
                const match = pathToRegexp('/home').exec(location.pathname);
            });
        },
        setup ({ dispatch }) {},
    },
    effects: {
        //获取巡检点列表
        *getCheck ({ payload }, { call, put }){
            const {data,total} = yield call(getCheck, payload);
            localStorage.removeItem('hrefid');
            if (payload.route_id){
                yield put({ type: 'save', payload: {getCheckData:data,total:total,pages:payload.pageNumber,Pagination:false}});
            }else {
                yield put({ type: 'save', payload: {getCheckData:data,total:total,pages:payload.pageNumber,Pagination:true}});
            }
        },
        //添加巡检点列表
        *postCheck ({ payload }, { call, put }){
            const data = yield call(postCheck, payload);
            if(data.code == 0){
                yield put(routerRedux.push('/checkpoint'));
                yield put({ type: 'save', payload: {pages:0}});
            }
        },
        //修改巡检列表
        *putCheck ({ payload }, { call, put }){
            const data = yield call(putCheck, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0}});
                yield put(routerRedux.push('/checkpoint'));
            }
        },
        //删除巡检点列表
        *deleteChack ({ payload }, { call, put }){
            const data = yield call(deleteChack, payload);
            if(data.code == 0){
                yield put({ type: 'save', payload: {pages:0}});
            }
        },
        //去详情
        *goList ({ payload }, { call, put }){
            const data = yield call(getInspep, {
                item_num: "",
                item_name :"",
                item_type: '',
                pageNumber :0,
                pageSize :0,
            });
            const dataTow = yield call(getPoint, {
                route_name: "",
                route_num :"",
                duty_name:"",
                pageNumber :0,
                pageSize :0,
            });
            yield put({ type: 'save', payload: {local:dataTow.data,localXM:data.data,record:payload.record}});
            yield put(routerRedux.push('/checkpoint_list'));
        },
        //返回
        *goBack ({ payload }, { call, put }){
            yield put(routerRedux.push('/checkpoint'));
        },
        //输入等于输出
        *inputOut ({ payload }, { put }){
            yield put({ type: 'save', payload: payload });
        },
    },
    reducers: {
        save (state, { payload }) {
            return { ...state, ...payload };
        },
    },
};
